<?php
//检测表单字段是否为空
if ($_REQUEST['goodsname'] == '') {
    echo '<script>alert("请输入商品名称！");history.back()</script>';
    exit;
}
if ($_REQUEST['type'] == '') {
    echo '<script>alert("请输入商品类型！");history.back()</script>';
    exit;
}
if ($_REQUEST['oldprice'] == '') {
    echo '<script>alert("请输入商品原价格！");history.back()</script>';
    exit;
}
if ($_REQUEST['price'] == '') {
    echo '<script>alert("请输入商品现价格！");history.back()</script>';
    exit;
}
if ($_REQUEST['desc'] == '') {
    echo '<script>alert("请输入商品描述！");history.back()</script>';
    exit;
}

$imgTmp = $_FILES['picture']['tmp_name'];
$imgPath = 'images/' . gmmktime() . '.jpg';
move_uploaded_file($imgTmp, '../' . $imgPath);

$goods_name = $_REQUEST['goodsname'];
$type = $_REQUEST['type'];
$old_price = $_REQUEST['oldprice'];
$description = $_REQUEST['desc'];
$price = $_REQUEST['price'];

include '../conn.php';
$sql = "insert into goods(goods_name,type,price,description,old_price,picture)"
    . " values('" . $goods_name . "','" . $type . "'," . $price . ",'" . $description . "',"
    . $old_price . ",'" . $imgPath . "')";

if ($conn->query($sql) === TRUE) {

    header("Location: goodslist.php");

} else {
    echo '添加商品失败！';
}
